<?php

namespace Home\Controller;

use Think\Controller;

class CommController extends Controller
{
    public $sevals;
    public $uinfos;
    public $ic_u_type;
    public $isbn;

    public function _initialize()
    {

        if (session("userAffline") == null && empty(cookie("user_affline"))) {
            $this->redirect("Index/index");
        }
        if(!empty(cookie("user_affline"))){
            session("userAffline",cookie("user_affline"));
        }

        $this->sevals = session("userAffline");
        $type = $this->sevals["sdk"] == null ? $this->sevals["type"] : $this->sevals["sdk"];
        $this->ic_u_type = $type;
        $this->uinfos = $this->getuserinfo($this->sevals["phone"], $type);
        $this->getusermenu($type);
        $this->getusercontrol($type);
        $this->assign("ph", $type == "2301" || $type == "2302" ? $this->uinfos["sag_phone"] : $this->uinfos["ssa_phone"]);
        $this->assign("names", $type == "2301" || $type == "2302" ? $this->uinfos["sag_name"] : $this->uinfos["ssa_name"]);
    }

    public function transtype()
    {
        if ($this->sevals["sdk"] == null) {
            $type = $this->sevals["type"] == "2301" || $this->sevals["type"] == "2302" ? 21 : $this->sevals["type"];
        } else {
            $type = $this->sevals["type"];
        }
        $info = $this->getuserinfo($this->sevals["phone"], $type);
        if ($info == null) {
            $this->error("您没有权限切换账户哦!");
        }
        session("userAffline", array(
            "phone" => $this->sevals["phone"],
            "type" => $this->sevals["type"],
            "sdk" => $this->sevals["type"] == $type ? null : $type
        ));
        switch ($type) {
            case '21':$this->redirect("Bord/index");
                break;
            case '2301':$this->redirect("Group/index");
                break;
            case '2302':$this->redirect("Group/index");
                break;
            default: $this->error("系统错误");
        }
    }

    /**
     * 依据不同类型获取对应的用户信息
     * @param type $phone
     */
    private function getuserinfo($phone, $type)
    {
        $db = M($type == "2301" || $type == "2302" ? "saleagent" : "salestaff");
        $where = $type == "2301" || $type == "2302" ? array("sag_phone" => $phone) : array("ssa_phone" => $phone);
        $info = $db->where($where)->find();
        if ($type == "21" || $type == "23") {
            $disisbn = M("disisbn");
            $isbn = $disisbn->where("dis_ssaid=" . $info["ssa_id"])->find();
            $this->isbn = $isbn;
        }
        return $info;
    }

    /**
     * 依据不同类型展示不同模板
     */
    private function getusermenu($type)
    {
        $powertempl = M("powertempl");
        $list = $powertempl->where("pot_status=1")->order("pot_sort asc")->select();
        for ($i = 0; $i < count($list); $i++) {
            if (in_array($type, explode(",", $list[$i]['pot_type']))) {
                $arr[] = $list[$i];
            }
        }
        $this->assign("menu", $arr);
    }

    /**
     * 权限控制
     */
    private function getusercontrol($type)
    {
        $powertempl = M("powertempl");
        $conf = CONTROLLER_NAME . "/" . ACTION_NAME;
        $where = array("pot_url" => $conf);
        $info = $powertempl->where($where)->find();
        $code = explode(",", $info['pot_type']);
        array_unshift($code, "NULC");
        if (array_search($type, $code) == false) {
            $this->error("没有权限");
        }
    }
}
